import 'package:flutter/material.dart';
import 'package:flutter_douban/config/Http_service.dart';
import 'package:flutter_douban/config/app_colors.dart';
import 'package:flutter_douban/model/cook_info_model.dart';
import 'package:flutter_douban/tool_views/cached_network_Image.dart';
import 'package:flutter_douban/tool_views/stance_page.dart';

import 'cook_detail_page.dart';

class AuthorCookPage extends StatefulWidget {
  final int id;
  const AuthorCookPage({Key? key,required this.id}) : super(key: key);

  @override
  _AuthorCookPageState createState() => _AuthorCookPageState();
}

class _AuthorCookPageState extends State<AuthorCookPage> with AutomaticKeepAliveClientMixin {
  /// 列表数组模型
  List<CookInfoModel> _cookInfoList = CookInfoModelList([]).list;

  // 获取数据
  Future _getRecommendData({bool replace = true}) async {
    try {
      /// 全部数据
      Map<String, dynamic> result =
          await HttpService.getAuthorCookListData(id: widget.id);
      CookInfoModelList infoList =
          CookInfoModelList.fromJson(result['recipes']);
      setState(() {
        _cookInfoList = infoList.list;
      });
    } catch (e) {
      setState(() {});
    } finally {
      setState(() {});
    }
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _getRecommendData();
  }

  @override
  Widget build(BuildContext context) {
    if(_cookInfoList.length == 0){
      return StancePage();
    }else{
      return Container(
        color: Colors.white,
        child: ListView.builder(
            shrinkWrap: true,
            ///禁用滑动事件
            physics: AlwaysScrollableScrollPhysics(),
            itemCount: _cookInfoList.length+1,
            itemBuilder: (BuildContext context, int index) {
              if (index == 0) {
                return Container(
                  margin: EdgeInsets.only(bottom: 0, left: 15, right: 15),
                  padding: EdgeInsets.only(left: 10,right: 10),
                  alignment: Alignment.centerLeft,
                  height: 36,
                  decoration: BoxDecoration(
                    color: AppColors.page,
                    borderRadius: BorderRadius.circular(20),
                  ),
                  child: Row(
                    children: [
                      Image(
                        image:
                        AssetImage("assets/images/common/button_search_gray.png"),
                        fit: BoxFit.cover,
                        width: 16,
                        height: 16,
                      ),
                      SizedBox(
                        width: 5,
                      ),
                      Text(
                        '搜索用户菜谱',
                        style: TextStyle(
                          color: Colors.black26,
                          fontSize: 14,
                        ),
                      )
                    ],
                  ),
                );
              } else {
                return _itemCell(index - 1);
              }
            }),
      );
    }
  }

  Widget _itemCell(int index) {
    return InkWell(
      child: Container(
        color: Colors.white,
        padding: EdgeInsets.only(top: 20),
        child: Column(
          children: [
            CachedImage(
                url:
                _cookInfoList[index].img!,
                width: MediaQuery.of(context).size.width,
                height: 210),
            SizedBox(
              height: 5,
            ),
            Container(
              padding: EdgeInsets.only(left: 15, right: 15),
              alignment: Alignment.centerLeft,
              child: Text(
                _cookInfoList[index].recommend_label!,
                style: TextStyle(fontSize: 12, color: Colors.black),
              ),
            ),
            SizedBox(
              height: 5,
            ),
            Container(
              padding: EdgeInsets.only(left: 15, right: 15),
              alignment: Alignment.centerLeft,
              child: Text(
                _cookInfoList[index].n!,
                style: TextStyle(fontSize: 14, color: Colors.black),
              ),
            ),
          ],
        ),
      ),
      onTap: () {
        Navigator.of(context).push(MaterialPageRoute(builder: (_) {
          return CookDetailPage(id: _cookInfoList[index].id!);
        }));
      },
    );
  }

  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;
}
